home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 031a / winbang.zip / BANG.C < prev    next >
C/C++ Source or Header  |  1991-12-03  |  14KB  |  482 lines

  1. /* ----- BANG.C -------------------------------------------------------------
  2.  *
  3.  *   DESCRIPTION:
  4.  *
  5.  *            Sample Program for Destroying WinDoom errors
  6.  *            (that means everything)
  7.  *
  8.  *   REMARKS:
  9.  *            Needed files are only the resource file PENG.RES.
  10.  *            Compilation without explicit .DEF file.
  11.  *
  12.  *   CHANGES:
  13.  *
  14.  *          V0.1    09-11-91    Heiner Eichmann
  15.  *                             & Michael Schuschk
  16.  *            Desperate start of programming.
  17.  *
  18.  *        V0.2    10-11-91    Heiner Eichmann
  19.  *                             & Jürgen Christ
  20.  *            Cleaned up program code and brought to first time
  21.  *            functioning (a real strong performance for a
  22.  *            pascal programmer). Shooting within and without the
  23.  *            window is possible.
  24.  *
  25.  *            V0.25    12-11-91    Michael Schuschk
  26.  *            Fixed bug with crippled shots appearing when window
  27.  *            is iconized and resized again. Screen is saved at
  28.  *            program start and will be restored before ending the
  29.  *            program.
  30.  *
  31.  *          V0.3    22-11-91    Heiner Eichmann
  32.  *            There's only an ABOUT dialog box and the sound
  33.  *            missing. All other things are the way we wanted
  34.  *            them.
  35.  *
  36.  *            V0.35    24-11-91    Michael Schuschk
  37.  *            ABOUT box with informations about this ultimate
  38.  *            program added. Window size at first start is
  39.  *            now depending on the width of the text lines
  40.  *            appearing within the window.
  41.  *
  42.  *          V0.4    25-11-91    Heiner Eichmann
  43.  *                Only slight corrections. Translation of text and
  44.  *            comments into english language. Now only the sound
  45.  *            is missing.
  46.  *
  47.  *        V0.5    26-11-91    Michael Schuschk
  48.  *                Slight corrections on the ABOUT box and shooting
  49.  *            hole design. And new in this version: the first
  50.  *            appearance of sound! But until now it sounds only
  51.  *            like a gun with sound absorber. This "pop" will
  52.  *                possibly be caused by the StartSound() and
  53.  *            StopSound() functions.
  54.  *        V0.6    27-11-91    Jürgen Christ & Heiner Eichmann
  55.  *            New cursor created. The word WinDoom was discovered
  56.  *            (Many thanks to Holger Eichmann). A small problem
  57.  *            with the distribution of the painted holes around
  58.  *            the cross-hair if fixed. The About About... window
  59.  *            was created. Problems with the aboutbitmap.
  60.  *          V1.0    03-12-91    Heiner Eichmann
  61.  *            Problems with the aboutabout-resource ignored. Light
  62.  *            changes of the sound-commands. Version number changed
  63.  *            to 1.0!!!!! Install and document created. Program
  64.  *            is now ready to mail.
  65.  *
  66.  *
  67.  *   INCLUDES:
  68.  */
  69. #include <windows.h>
  70. #include <stdlib.h>
  71. /*
  72.  *   DEFINES:
  73.  */
  74.  
  75. #define STARTPOSX     CW_USEDEFAULT
  76. #define STARTPOSY     CW_USEDEFAULT
  77. #define RANGEXDIV     20
  78. #define RANGEYDIV     20
  79. #define FIREDELAY     30
  80. #define HALF_SIZE    8      /* Size of icon LOCH div 2 */
  81. #define IDM_SYSABOUT    4242
  82. #define ID_OK        101
  83. #define ABOUTABOUT      6942
  84. /*
  85.  *   EXTERNAL:
  86.  */
  87. /*
  88.  *   TYPES:
  89.  */
  90. /*
  91.  *   VARIABLES:
  92.  */
  93. HCURSOR hcursor, gaehn_cursor;
  94. HANDLE  scheiss_c;
  95. HDC    hdcvar, hdcmem, hdcold;
  96. HBITMAP    hbitm, sbitm;
  97. POINT    ptSize, ptOrg;
  98. BITMAP    bm;
  99. short    cxSize, cySize;
  100. int    rangex, rangey;
  101. int    load_flag, treffer_flag;
  102. int    iMaxZeile;        /* Maximum width of text in pixel */
  103. short    cxChar, cxCaps, cyCaps;    /* Dimensions of one char of actual
  104.                    system font */
  105. #define LINENUMS     8
  106. char    acText[LINENUMS][100] = {
  107.     "Death to Mickeysoft: The program",
  108.     "everyone was waiting for",
  109.     "",
  110.     "Left mousebutton: Fire !",
  111.     "",
  112.     "Right mousebutton: Back to work.",
  113.     "",
  114.     "Written by REMBRANDT, POWER MIKE und McJOSH."};
  115. /*
  116.  *    PROTOTYPES
  117.  */
  118. int PASCAL WinMain (HANDLE, HANDLE, LPSTR, int);
  119. long FAR PASCAL _export WindowProcedure (HWND, unsigned, WORD, LONG);
  120. BOOL FAR PASCAL AboutBox(HWND, unsigned, WORD, LONG);
  121. /*
  122.  *   RETURN:
  123.  *
  124.  *   CALL AND PARAMETERS:
  125.  */
  126. /*
  127.  *
  128.  * ------------------------------------------------------------------------ */
  129.  
  130. /* ----- WinMain ----------------------------------------------------------
  131.  *
  132.  *    Windows Entry Function
  133.  * ---------------------------------------------------------------------- */
  134.  
  135. int PASCAL WinMain (hInstance, hPrevInstance, lpszCmdLine, nCmdShow)
  136.  
  137. HANDLE    hInstance;    /* Actual instance */
  138. HANDLE    hPrevInstance;    /* Preious instance */
  139. LPSTR    lpszCmdLine;    /* Command line parameter */
  140. int    nCmdShow;    /* Window or icon? */
  141. {
  142.     HWND        hWindow;        /* Window handle */
  143.     HMENU        hSysMenu;        /* System menu handle */
  144.     MSG        Message;        /* Message struct */
  145.     WNDCLASS    WindowClass;        /* Window class */
  146.     TEXTMETRIC    tm;            /* Text dimensions */
  147.     HDC        hdcSystemDC;        /* Screen display context */
  148.     static char    acName[] = "hallo";    /* Window name */
  149.     int        i;            /* Counter, what else? */
  150.  
  151.     /* Already an application of the same type running? */
  152.     scheiss_c = hInstance;
  153.     load_flag = -1;
  154.     treffer_flag = 0;
  155.     if (!hPrevInstance) {
  156.         WindowClass.style = CS_VREDRAW | CS_HREDRAW;
  157.         WindowClass.lpfnWndProc = WindowProcedure;
  158.         WindowClass.cbClsExtra = 0;
  159.         WindowClass.cbWndExtra = 0;
  160.         WindowClass.hInstance = hInstance;
  161.         WindowClass.hIcon = LoadIcon (hInstance, (LPSTR) "MegaIcon");
  162.         WindowClass.hCursor = LoadCursor (hInstance, (LPSTR) "GAEHN");
  163.         WindowClass.hbrBackground = GetStockObject (WHITE_BRUSH);
  164.         WindowClass.lpszMenuName = NULL;
  165.         WindowClass.lpszClassName = acName;
  166.  
  167.         /* Already existing a window of the same class? */
  168.         if (!RegisterClass (&WindowClass))
  169.             return (FALSE);
  170.  
  171.     }
  172.  
  173.     /* Get dimensions of an capital char of the actual system font */
  174.     hdcSystemDC = CreateDC("DISPLAY", NULL, NULL, NULL);
  175.     GetTextMetrics(hdcSystemDC, &tm);
  176.     cxChar = tm.tmAveCharWidth;
  177.     cxCaps = (tm.tmPitchAndFamily & 1 ? 3 : 2) * cxChar / 2;
  178.     cyCaps = tm.tmHeight + tm.tmExternalLeading;
  179.  
  180.     /* Get maximum with of one text row in pixels */
  181.     iMaxZeile = 0;
  182.     for (i = 0; i < LINENUMS; i++)
  183.         iMaxZeile = max(iMaxZeile, LOWORD(GetTextExtent(hdcSystemDC,
  184.             acText[i], strlen(acText[i]))));
  185.  
  186.     DeleteDC(hdcSystemDC);
  187.  
  188.     /* Create window */
  189.     hWindow = CreateWindow (acName,            /* Window class name */
  190.                 "Death to Mickeysoft",    /* Window title */
  191.                 WS_OVERLAPPED        /* Window style */
  192.                 |WS_CAPTION|WS_SYSMENU
  193.                 |WS_MINIMIZEBOX,
  194.                 STARTPOSX,         /* Window start */
  195.                 STARTPOSY,         /* position and */
  196.                 iMaxZeile + 10,        /* size */
  197.                 cyCaps * (LINENUMS + 2),
  198.                 NULL,
  199.                 NULL,
  200.                 hInstance,
  201.                 NULL);
  202.  
  203.     /* Append menu entry for About... function */
  204.     hSysMenu = GetSystemMenu(hWindow, FALSE);
  205.     AppendMenu(hSysMenu, MF_SEPARATOR, 0, NULL);
  206.     AppendMenu(hSysMenu, MF_STRING, IDM_SYSABOUT, "About Bang...");
  207.     AppendMenu(hSysMenu, MF_STRING, ABOUTABOUT, "About About...");
  208.  
  209.     /* Show Window */
  210.     ShowWindow (hWindow, SW_SHOWMINIMIZED);
  211.     UpdateWindow (hWindow);
  212.     hcursor = LoadCursor(hInstance, "MegaCursor");
  213.     gaehn_cursor = LoadCursor(hInstance, "GAEHN");
  214.     sbitm = LoadBitmap(hInstance, "LOCH");
  215.  
  216.     /* Setup sound queue */
  217.     OpenSound();
  218.     SetSoundNoise(S_WHITE2048, 5);
  219.     SetVoiceQueueSize(1, 1000);
  220.     SetVoiceAccent(1, 200, 30, S_NORMAL, 40);
  221.  
  222.     /* message loop */
  223.     while (GetMessage (&Message, NULL, 0, 0) ){
  224.         TranslateMessage (&Message);
  225.         DispatchMessage (&Message);
  226.     }
  227.  
  228.     return (Message.wParam);
  229.  
  230. }    /* this was win main ... */
  231.  
  232. /* ----- BallerBumm -----------------------------------------------------
  233.  *
  234.  *    Do the great bang one time
  235.  * ---------------------------------------------------------------------- */
  236. void    krawumm()
  237. {
  238.     int     posx, posy;
  239.     int    i;
  240.     POINT     ptcursorpos;
  241.  
  242.     GetCursorPos((LPPOINT)&ptcursorpos);
  243.     posx = ptcursorpos.x - rangex / 2 - HALF_SIZE;
  244.     posy = ptcursorpos.y - rangey / 2 - HALF_SIZE;
  245.     treffer_flag = -1;
  246.  
  247.     /* Display shooting hole */
  248.     BitBlt(hdcvar, posx+random(rangex), posy+random(rangex), ptSize.x,
  249.         ptSize.y, hdcold, ptOrg.x, ptOrg.y, SRCAND);
  250.  
  251.     /* And make the appropriate noise */
  252.     SetVoiceSound(1, (long)(500) << 16, 1);
  253.     StartSound();/* MessageBeep(0);*/
  254. }
  255. /* ---------------------------------------------------------------------- */
  256.  
  257. long FAR PASCAL _export DummyProcedure (hWnd, uiMessage, wParam, lParam)
  258.  
  259. HWND        hWnd;        /* Window handle */
  260. unsigned    uiMessage;    /* Message number */
  261. WORD        wParam;        /* wParam and lParam contain */
  262. LONG        lParam;        /* additional informations */
  263. {
  264.     return (DefWindowProc (hWnd, uiMessage, wParam, lParam));
  265. }
  266.  
  267. /* The following procedure is obviously necessary to update the whole screen.
  268.    At the moment there is no other method known to me. */
  269. void    restorescreen()
  270. {
  271.     HWND        dummy;
  272.     WNDCLASS    WindowClass;        /* Window class */
  273.  
  274.     WindowClass.style = CS_VREDRAW | CS_HREDRAW;
  275.     WindowClass.lpfnWndProc = DummyProcedure;
  276.     WindowClass.cbClsExtra = 0;
  277.     WindowClass.cbWndExtra = 0;
  278.     WindowClass.hInstance = scheiss_c;
  279.     WindowClass.hIcon = NULL;
  280.     WindowClass.hCursor = NULL;
  281.     WindowClass.hbrBackground = GetStockObject (WHITE_BRUSH);
  282.     WindowClass.lpszMenuName = NULL;
  283.     WindowClass.lpszClassName = "rembrandt";
  284.  
  285.     /* Already a window of the same class existing? */
  286.     RegisterClass (&WindowClass);
  287.     if (treffer_flag)
  288.     {
  289.         dummy = CreateWindow ("rembrandt",
  290.             "Death to Mickeysoft",
  291.             WS_OVERLAPPED,
  292.             0,
  293.             0,
  294.             cxSize,
  295.             cySize,
  296.             NULL,
  297.             NULL,
  298.             scheiss_c,
  299.             NULL);
  300.         ShowWindow(dummy, SW_SHOWMAXIMIZED);
  301.         InvalidateRgn(dummy,NULL, TRUE);
  302.         DestroyWindow(dummy);
  303.         treffer_flag = 0;
  304.     }
  305. }
  306.  
  307.  
  308.  
  309. long FAR PASCAL _export WindowProcedure (hWnd, uiMessage, wParam, lParam)
  310.  
  311. HWND        hWnd;        /* Window handle */
  312. unsigned    uiMessage;    /* Message number */
  313. WORD        wParam;        /* wParam and lParam contain */
  314. LONG        lParam;        /* additional informations */
  315. {
  316.     HDC        hDisplayContext;    /* Display context handle
  317.                            for window function */
  318.     PAINTSTRUCT    PaintStructure;        /* Info for window
  319.                            repainting */
  320.     int         i;
  321.     static int    ballerbumm = 0;
  322.     static int    cxClientArea, cyClientArea;
  323.                         /* Width and height of
  324.                            client area */
  325.     static int    cyOffset;        /* Vertical offset for text
  326.                            display */
  327.     FARPROC        lpAboutProc;        /* Pointer to the About
  328.                            dialog box function */
  329.  
  330.     switch (uiMessage){
  331.     case WM_CREATE :
  332.             /* Get screen dimensions */
  333.             cxSize = GetSystemMetrics(SM_CXSCREEN);
  334.             cySize = GetSystemMetrics(SM_CYSCREEN);
  335.  
  336.             /* Back to where the action is... */
  337.             return(0);
  338.     case WM_SIZE :
  339.             /* Get client area width and height for centered
  340.                text display */
  341.             cxClientArea = LOWORD(lParam);
  342.             cyClientArea = HIWORD(lParam);
  343.  
  344.             if (wParam == SIZENORMAL) {
  345.                 SetCursor(hcursor);
  346.                 SetCapture(hWnd);
  347.                 ballerbumm = 1;
  348.                 hdcvar = CreateDC("DISPLAY", NULL, NULL, NULL);
  349.                 hdcmem = CreateCompatibleDC(hdcvar);
  350.                 cxSize = GetSystemMetrics(SM_CXSCREEN);
  351.                 rangex = cxSize / RANGEXDIV;
  352.                 cySize = GetSystemMetrics(SM_CYSCREEN);
  353.                 rangey = cySize / RANGEYDIV;
  354.                 hbitm = CreateCompatibleBitmap(hdcvar,
  355.                     cxSize, cySize);
  356.                 SelectObject(hdcmem, hbitm);
  357.                 hdcold = CreateCompatibleDC(hdcvar);
  358.                 SelectObject(hdcold, sbitm);
  359.                 SetMapMode(hdcold, GetMapMode(hdcvar));
  360.  
  361.                 GetObject(sbitm, sizeof(BITMAP), (LPSTR) &bm);
  362.                 ptSize.x = bm.bmWidth;
  363.                 ptSize.y = bm.bmHeight;
  364.                 DPtoLP(hdcvar, &ptSize, 1);
  365.  
  366.                 ptOrg.x = 0;
  367.                 ptOrg.y = 0;
  368.                 DPtoLP (hdcmem, &ptOrg, 1);
  369.             }
  370.             if (wParam == SIZEICONIC) {
  371.  
  372.                 /* And now delete the bunch of device
  373.                    contexts, because there are only 6 of
  374.                    them system wide */
  375.                 if (load_flag)
  376.                     load_flag = 0;
  377.                 else
  378.                 {
  379.                     DeleteDC(hdcvar);
  380.                     DeleteDC(hdcmem);
  381.                     DeleteDC(hdcold);
  382.                     restorescreen();
  383.                 }
  384.             }
  385.         break;
  386.     case WM_PAINT :
  387.         hDisplayContext = BeginPaint (hWnd, &PaintStructure);
  388.  
  389.         /* Get vertical offset for text display */
  390.         cyOffset = max(0, cyClientArea - (LINENUMS * cyCaps));
  391.  
  392.         /* Display text centered in window */
  393.         for (i = 0; i < LINENUMS; i++){
  394.             SetTextAlign(hDisplayContext, TA_CENTER | TA_TOP);
  395.             TextOut (hDisplayContext, cxClientArea / 2,
  396.                 cyOffset+(i * cyCaps), acText[i],
  397.                 (int)strlen(acText[i]));
  398.             SetTextAlign(hDisplayContext, TA_LEFT | TA_TOP);
  399.         }
  400.  
  401.         EndPaint (hWnd, &PaintStructure);
  402.         break;
  403.     case WM_LBUTTONUP :
  404.         StopSound();
  405.         KillTimer(hWnd, 1);
  406.         return(0);
  407.     case WM_TIMER :
  408.         krawumm();
  409.         return(0);
  410.     case WM_LBUTTONDOWN :
  411.         if (ballerbumm) {
  412.             krawumm();
  413.             SetTimer(hWnd, 1, FIREDELAY, NULL);
  414.             return(0);
  415.         }
  416.         break;
  417.     case WM_RBUTTONDOWN :
  418.         if (ballerbumm) {
  419.             ballerbumm = 0;
  420.             StopSound();
  421.             ReleaseCapture();
  422.             SetCursor(gaehn_cursor);
  423.             return(0);
  424.         }
  425.         break;
  426.     case WM_SYSCOMMAND:
  427.         if (wParam == IDM_SYSABOUT){
  428.             lpAboutProc = MakeProcInstance(AboutBox, scheiss_c);
  429.             DialogBox(scheiss_c, "MEGAABOUT", hWnd,    lpAboutProc);
  430.             FreeProcInstance(lpAboutProc);
  431.         }
  432.         if (wParam == ABOUTABOUT){
  433.             lpAboutProc = MakeProcInstance(AboutBox, scheiss_c);
  434.             DialogBox(scheiss_c, "ABOUTABOUT", hWnd,    lpAboutProc);
  435.             FreeProcInstance(lpAboutProc);
  436.         }
  437.         break;
  438.     case WM_DESTROY :
  439.             DeleteDC(hdcvar);
  440.             DeleteDC(hdcmem);
  441.             DeleteDC(hdcold);
  442.             CloseSound();
  443.             restorescreen();
  444.             PostQuitMessage (0);
  445.  
  446.             /* Back to where the action is... */
  447.             return(0);
  448.     }
  449.     if (ballerbumm)
  450.         return(0);
  451.     else
  452.         return (DefWindowProc (hWnd, uiMessage, wParam, lParam));
  453.  
  454. }    /* this was the window function */
  455.  
  456. /* ----- AboutBox -------------------------------------------------------
  457.  *
  458.  * About box function
  459.  * ---------------------------------------------------------------------- */
  460. BOOL FAR PASCAL AboutBox(hDialog, uiMeldung, wParam, lParam)
  461. HWND        hDialog;    /* Dialog box handle */
  462. unsigned       uiMeldung;    /* Dialog box message */
  463. WORD        wParam;        /* wParam und lParam contain */
  464. LONG        lParam;        /* additional informations */
  465. {
  466.     switch (uiMeldung){
  467.  
  468.         /* Dialog box initialized: only default actions */
  469.         case WM_INITDIALOG:
  470.             return(TRUE);
  471.  
  472.         /* User likes to keep on firing */
  473.         case WM_COMMAND:
  474.             if (wParam == ID_OK){
  475.                 EndDialog(hDialog, TRUE);
  476.                 return(TRUE);
  477.             }
  478.             break;
  479.     }
  480.     return(FALSE);
  481. }
  482.